//创建vuex核心store
import Vue from 'vue'
// 引入vuex
import Vuex from 'vuex'
import { getCity, getCitySort } from "../api/city";

// 准备mutations 用于数据存储
const state = {
    arr: "",
    cityList: "",
    city: "西安",
    city1: '',
    flag: true
}
// 准备actions 用于响应组件中的动作
// 准备mutations 用于操作数据
const mutations = {
    //获取城市列表
    getCity(state, payload) {
        state.cityList = payload;
    },
    //  获取城市按字母排序
    getCitySort(state, payload) {
        state.arr = payload;
    },
    // 选择当前城市
    city(state, payload) {
        state.flag = true
        state.city = payload
        console.log(state.city);
    },
    // 按字母选择
    city1(state, payload) {
        state.flag = false
        state.city1 = payload
        console.log(state.city1);
    }
}
const actions = {
    //获取城市列表
    ChoiceCity(context) {
        getCity().then((res) => {
            // console.log(res.data);
            context.commit('getCity', res.data)
        });
    },
    // // 获取城市按字母排序
    GetCitySort(context) {
        getCitySort().then((res) => {
            // console.log(res.data);
            context.commit('getCitySort', res.data)
        });
    }

}

Vue.use(Vuex)

//创建并暴露store
export default new Vuex.Store({
    actions,
    mutations,
    state,
})
